#include <project.h>
#include "userMain.h"

extern unsigned char  userKeyValue;
extern unsigned char userRelayStateNow;
unsigned char userKeyNeedSendFlag = 0;
extern uint8 userLightDefaultBindEnable;
extern uint8 userRelayDefaultBindEnable;
extern uint8 userBeepEnable;
extern uint8 userMotionEnable;
extern uint8 userNightState;
extern unsigned char uDistictLedFlag;
extern unsigned char userLedState;
extern unsigned char uLoadDelayCloseFlag;
extern unsigned char userAlarmFlag;
extern unsigned char userNightOperateState;
extern uint8 userLedStartFlag;
void userKeyProcess(void)
{
    if(userAlarmFlag)
    {
        userAlarmFlag = 0;
        LED_Write(userLedState);
    }
    if(userLedStartFlag)
    {
        if((userKeyValue != 0x05)&&(userKeyValue != 0x06)&&(userKeyValue != 0x07))
            return ;
    }
    switch(userKeyValue)
    {
        case 0x01://Distict 1 Short Press
        case 0x02://Distict 2 Short Press
        case 0x03://Distict 3 Short Press
        case 0x04://Distict 4 Short Press
        {
            userKeyNeedSendFlag = 1;
            userSendKeyInfoFunc();
            //userSendKeyInfoFunc();
            //userSendNodeRegisterInfoFunc();
            //userSendKeyInfoFunc();
            //userSendKeyInfoFunc();
            //userSendNodeRegisterInfoFunc();
            userBeepSetFunc(1,1);
            break;
        }
        case 0x81://Distict 1 Long Press
        case 0x82://Distict 2 Long Press
        case 0x83://Distict 3 Long Press
        case 0x84://Distict 4 Long Press
        {
            userKeyNeedSendFlag = 1;
            userSendKeyInfoFunc();
            userBeepSetFunc(1,2);
            userDistictLedOperate(2);
            break;
        }
        case 0x05://Distict 5 Short Press
        {
            if(userLedStartFlag==0)
            userBeepSetFunc(1,1);
            if(uLoadDelayCloseFlag&0x01)
            {
                uLoadDelayCloseFlag &= 0xFE;
                userLedCtrlFunc(5,1);
                userKeyNeedSendFlag = 1;
                userSendKeyInfoFunc();
                return ;
            }
            //userLedCtrlFunc(5,3);   //Back LED control
            userLedProcessFunc();
            userRelayCtrlFunc(2,0x2B); 
            userKeyNeedSendFlag = 1;
            userSendKeyInfoFunc();
            break;
        }
        case 0x85://Distict 5 Long Press
        {
            if(userLedState&0x01)
            {
                userLedCtrlFunc(5,4);   //Back LED control
                userKeyNeedSendFlag = 1;
                userSendKeyInfoFunc();
                userBeepSetFunc(1,2);
                userLoadDelayCloseSetFunc(1);
                break;
            }
            userBeepSetFunc(0,1);
            break;
        }
        case 0x06://Distict 6 Short Press
        {
            if(userLedStartFlag==0)
            userBeepSetFunc(1,1);
            if(uLoadDelayCloseFlag&0x02)
            {
                uLoadDelayCloseFlag &= 0xFD;
                userLedCtrlFunc(6,1);
                userKeyNeedSendFlag = 1;
                userSendKeyInfoFunc();
                return ;
            }
            //userLedCtrlFunc(6,3);
            userLedProcessFunc();
            userRelayCtrlFunc(2,0x2E);
            userKeyNeedSendFlag = 1;
            userSendKeyInfoFunc();
            break;
        }
        case 0x86://Distict 6 Long Press
        {
            if(userLedState&0x02)
            {
                userLedCtrlFunc(6,4);   //Back LED control
                userKeyNeedSendFlag = 1;
                userSendKeyInfoFunc();
                userBeepSetFunc(1,2);
                userLoadDelayCloseSetFunc(2);
                break;
            }
            userBeepSetFunc(0,1);
            break;
        }
        case 0x07://Distict 7 Short Press
        {
            if(userLedStartFlag==0)
            userBeepSetFunc(1,1);
            if(uLoadDelayCloseFlag&0x04)
            {
                uLoadDelayCloseFlag &= 0xFB;
                userLedCtrlFunc(7,1);
                userKeyNeedSendFlag = 1;
                userSendKeyInfoFunc();
                return ;
            }
            //userLedCtrlFunc(7,3);
            userLedProcessFunc();
            userRelayCtrlFunc(2,0x3A);
            userKeyNeedSendFlag = 1;
            userSendKeyInfoFunc();
            break;
        }
        case 0x87://Distict 7 Long Press
        {
            if(userLedState&0x04)
            {
                userLedCtrlFunc(7,4);   //Back LED control
                userKeyNeedSendFlag = 1;
                userSendKeyInfoFunc();
                userBeepSetFunc(1,2);
                userLoadDelayCloseSetFunc(3);
                break;
            }
            userBeepSetFunc(0,1);
            break;
        }
        case 0x95://Local 1 + Distict 1 Long Press
        {
            userKeyNeedSendFlag = 0;
            userBeepSetFunc(1,2);
            userDistictLedOperate(2);
            userRelayDefaultBindEnable = !userRelayDefaultBindEnable;
            //userSendNodeRegisterInfoFunc();
            userSendSensorConfigInfoFunc();
            userMotionRelayBindFunc();
            //CyDelay(6);
            break;
        }
        case 0x92://Distict 1 + Distict 2 Long Press
        {
            userBeepSetFunc(1,2);
            userKeyNeedSendFlag = 0;
            userDistictLedOperate(2);
            userLightDefaultBindEnable = !userLightDefaultBindEnable;
            //userSendNodeRegisterInfoFunc();
            userSendSensorConfigInfoFunc();
            userMotionNightBindFunc();
            //CyDelay(6);
            break;
        }
        case 0x93://Distict 1 + Distict 3 Long Press
        {
            userBeepEnable = !userBeepEnable;
            userBeepSetFunc(1,2);
            userKeyNeedSendFlag = 0;
            userDistictLedOperate(2);
            //userBeepEnable = !userBeepEnable;
            //userSendNodeRegisterInfoFunc();
            userSendEnableInfoFunc();
            //CyDelay(6);
            break;
        }
        case 0x94://Distict 1 + Distict 4 Long Press
        {
            userBeepSetFunc(1,2);
            userKeyNeedSendFlag = 0;
            userDistictLedOperate(2);
            userMotionEnable = !userMotionEnable;
            //userSendNodeRegisterInfoFunc();
            userSendEnableInfoFunc();
            //CyDelay(6);
            break;
        }
        case 0xA2://Distict 2 + Distict 3 Long Press
        {
            userBeepSetFunc(1,2);
            userKeyNeedSendFlag = 0;
            userDistictLedOperate(2);
            if(userNightState)
            {
                userNightOff();
                userNightOperateState=0x00;
            }
            else
            {
                userNightOn();
                userNightOperateState=0x01;
            }
            //userSendNodeRegisterInfoFunc();
            userSendNightInfoFunc();
            break;
        }
        case 0x0c:
        case 0x8C:
        {
            userKeyNeedSendFlag = 0;
            if(userRelayStateNow)
            {
                userRelayCtrlFunc(2,0x00);
                userLedCtrlFunc(5,0);
                userLedProcessFunc();
                userLedCtrlFunc(6,0);
                userLedProcessFunc();
                userLedCtrlFunc(7,0);
                userLedProcessFunc();
            }
            else
            {
                userRelayCtrlFunc(2,0x15);
                userLedCtrlFunc(5,1);
                userLedProcessFunc();
                userLedCtrlFunc(6,1);
                userLedProcessFunc();
                userLedCtrlFunc(7,1);
                userLedProcessFunc();
            }
            userBeepSetFunc(1,1);
            //userSendRelayInfoFunc();
            break;
        }
        default:
        {
            break;
        }
    }
    
}



